package com.bolt.admin.module.sys.service.converter;

import com.bolt.admin.module.sys.dto.PrivilegeDTO;
import com.bolt.admin.module.sys.entity.PrivilegeEntity;
import com.bolt.common.reflect.ObjectUtil;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2021/3/5.
 */
public class PrivilegeConverter {
    private PrivilegeConverter() {
// do nothing
    }

    /**
     * Entity对象转换为DTO对象
     *
     * @param privilege DO对象
     * @return DTO对象
     */
    public static PrivilegeDTO toDTO(PrivilegeEntity privilege) {
        if (Objects.isNull(privilege)) {
            return null;
        }
        PrivilegeDTO result = new PrivilegeDTO();
        result.setId(privilege.getId());
        result.setCode(privilege.getCode());
        result.setEnabled(privilege.getEnabled());
        result.setMenuId(privilege.getMenuId());
        result.setOrderRank(privilege.getOrderRank());
        result.setType(privilege.getType());
        result.setTitle(privilege.getTitle());
        result.setVersionNo(privilege.getVersionNO());
        result.setPid(privilege.getPid());
        result.setChildrenSize(privilege.getChildrenSize());
        result.setInheritLevel(privilege.getLevel());
        // TODO
        return result;
    }

    /**
     * DTO对象转换为Entity对象
     *
     * @param privilege DO对象
     * @return VO对象
     */
    public static PrivilegeEntity toEntity(PrivilegeDTO privilege) {
        if (Objects.isNull(privilege)) {
            return null;
        }
        PrivilegeEntity result = new PrivilegeEntity();
        result.setId(privilege.getId());
        result.setCode(privilege.getCode());
        result.setEnabled(privilege.getEnabled());
        result.setMenuId(privilege.getMenuId());
        result.setOrderRank(privilege.getOrderRank());
        result.setType(privilege.getType());
        result.setTitle(privilege.getTitle());
        result.setVersionNO(privilege.getVersionNo());
        result.setPid(privilege.getPid());
        if (ObjectUtil.isNotNull(privilege.getPid())) {
            result.setParent(new PrivilegeEntity(privilege.getPid()));
        }
        result.setChildrenSize(privilege.getChildrenSize());
        result.setLevel(privilege.getInheritLevel());
        // TODO
        return result;
    }

    /**
     * DTO对象转换为DO对象
     *
     * @param privilege DO对象
     * @return VO对象
     */
    public static PrivilegeEntity toEntity(PrivilegeEntity result, PrivilegeDTO privilege) {
        if (Objects.isNull(privilege)) {
            return null;
        }

        result.setCode(privilege.getCode());
        result.setEnabled(privilege.getEnabled());
        result.setMenuId(privilege.getMenuId());
        result.setOrderRank(privilege.getOrderRank());
        result.setType(privilege.getType());
        result.setTitle(privilege.getTitle());
        result.setVersionNO(privilege.getVersionNo());
        result.setPid(privilege.getPid());
        result.setChildrenSize(privilege.getChildrenSize());
        result.setLevel(privilege.getInheritLevel());
// TODO
        return result;
    }

    /**
     * 批量 Entity对象转换为DTO对象
     *
     * @param privilegeList Entity集合对象
     * @return DTO对象
     */
    public static List <PrivilegeDTO> toDTO(List <PrivilegeEntity> privilegeList) {
        if (Objects.isNull(privilegeList)) {
            return null;
        }
        return privilegeList.stream().map(PrivilegeConverter::toDTO).collect(Collectors.toList());
    }
}

 